package com.lin.activity;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dlazaro66.qrcodereaderview.QRCodeReaderView;
import com.dlazaro66.qrcodereaderview.QRCodeReaderView.OnQRCodeReadListener;
import com.lin.utils.Net;
import com.lin.utils.Session;
import com.lin.utils.Session.ResultReceiver;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.bean.Constants;

public class DecoderActivity extends Activity implements OnQRCodeReadListener {

    private TextView myTextView;
	private QRCodeReaderView mydecoderview;
	private ImageView line_image;
	Session mSession;
	boolean isShowed = false;
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_decoder);
        
        mSession = Session.getSession();
        
        mydecoderview = (QRCodeReaderView) findViewById(R.id.qrdecoderview);
        mydecoderview.setOnQRCodeReadListener(this);
        
        myTextView = (TextView) findViewById(R.id.exampleTextView);
        
        line_image = (ImageView) findViewById(R.id.red_line_image);
        
        TranslateAnimation mAnimation = new TranslateAnimation(
                TranslateAnimation.ABSOLUTE, 0f,
                TranslateAnimation.ABSOLUTE, 0f,
                TranslateAnimation.RELATIVE_TO_PARENT, 0f,
                TranslateAnimation.RELATIVE_TO_PARENT, 1f);
       mAnimation.setDuration(1000);
       mAnimation.setRepeatCount(-1);
       mAnimation.setRepeatMode(Animation.REVERSE);
       mAnimation.setInterpolator(new LinearInterpolator());
       line_image.setAnimation(mAnimation);
        
    }

    private static final String[] strings = { "http" , "Http", "www"};
	@Override
	public void onQRCodeRead(String text, PointF[] points) {
		text = text.replace("http://superchat.api.yunfengapp.com:8989/", "");
		text = text.replace("http://app.chaoruan.net/ay.htm?", "");
		text = text.replace("http://app.chaoruan.net/?", "");
		
		if(isContainsList(text)) {
			Net.openBrower(this, text);
		} else {
			mSession.AddFriendSweep(getApplicationContext(), Session.getSession().getmUserBean().getUserid() + "", text+ "", new ResultReceiver<Integer>() {

				@Override
				public void onResult(boolean last, Integer args) {
					// TODO Auto-generated method stub
					if(args != -1) {
						if (!isShowed) {
							Toast.makeText(DecoderActivity.this, "添加好友成功", Toast.LENGTH_SHORT).show();
							isShowed = true;
						}
						sendBroadcast(new Intent(Constants.ACTION_NEWFRIEND_MSG));
						finish();
					}
				}
				
			});
		}
		
		myTextView.setText(text);
	}

	boolean isContainsList(String text) {
		for (String s : strings) {
			if (text.startsWith(s)) {
				return true;
			}
		}
		return false;
	}
	
	// Called when your device have no camera
	@Override
	public void cameraNotFound() {
		
	}

	// Called when there's no QR codes in the camera preview image
	@Override
	public void QRCodeNotFoundOnCamImage() {
		
	}
    
	@Override
	protected void onResume() {
		super.onResume();
		mydecoderview.getCameraManager().startPreview();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		mydecoderview.getCameraManager().stopPreview();
	}
}
